/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.editura.oxygen.severin.imageviewer.features.io.resources;

import de.editura.oxygen.severin.imageviewer.features.io.resources.KnownDatamodelMarshaller;
import de.editura.oxygen.severin.imageviewer.features.knownDatamodels.ImageRefField;
import de.editura.oxygen.severin.imageviewer.features.knownDatamodels.ImageRefFileTypes;
import de.editura.oxygen.severin.imageviewer.features.knownDatamodels.ImageRefXmlTypes;
import de.editura.oxygen.severin.imageviewer.features.knownDatamodels.KnownDatamodel;
import de.editura.oxygen.severin.imageviewer.features.knownDatamodels.KnownDatamodels;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author kschmidtner
 */
public class KnownDatamodelUnmarshaller {

    File kdmPathsDir = new File(System.getProperty("user.home") + "/OxygenExtension/SeverinImageViewer/");
    File kdmXmlFile = new File(kdmPathsDir, "knownDatamodels.xml");

    public List<KnownDatamodel> unmarshallKnownDatamodels() {

        List<KnownDatamodel> kdms = new ArrayList<KnownDatamodel>();
        if (!kdmXmlFile.exists()) {
            createKdmXmlFile();
        }
        try {
            JAXBContext ctx = JAXBContext.newInstance(KnownDatamodels.class);
            Unmarshaller um = ctx.createUnmarshaller();
            KnownDatamodels kdmsObj = (KnownDatamodels) um.unmarshal(kdmXmlFile);
            kdms = kdmsObj.getKdms();
        } catch (JAXBException ex) {
            Logger.getLogger(KnownDatamodelUnmarshaller.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kdms;
    }

    private void createKdmXmlFile () {

        //three samples for known data models
        
        // TEI - Text encoding Initiative
        List<ImageRefField> imgRefsRuSHA = new ArrayList<ImageRefField>();
        imgRefsRuSHA.add(new ImageRefField("facs", ImageRefXmlTypes.ATTRIBUTE, ImageRefFileTypes.FILE));
        KnownDatamodel kdmTEI = new KnownDatamodel("TEI", "pb", imgRefsRuSHA);

        // EAD
        List<ImageRefField> imgRefsEad = new ArrayList<ImageRefField>();
        imgRefsEad.add(new ImageRefField("xlink:href", ImageRefXmlTypes.ATTRIBUTE, ImageRefFileTypes.FILE));
        KnownDatamodel kdmEAD = new KnownDatamodel("EAD", "did", imgRefsEad);
        
        List<KnownDatamodel> kdmList = new ArrayList<KnownDatamodel>();
        kdmList.add(kdmTEI);
        kdmList.add(kdmEAD);

        if (!kdmPathsDir.exists()) {
            kdmPathsDir.mkdirs();
        }

        if (!kdmXmlFile.exists()) {
            KnownDatamodels kdms = new KnownDatamodels();
            kdms.setKdms(kdmList);
            KnownDatamodelMarshaller.marshallKnownDatamodels(kdms, kdmXmlFile);
            //Todo - Debug INFO: System.out.println("marshalled.");
        }
    }


}
